﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.DAL
{
    public class DrugDAL:IDrugDAL
    {
        #region IDrugDAL 成员

        public IDataReader Select(int DrugNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Reader(ConnectionString.ConnString, "Proc_Drug_Select", new SqlParameter[] { new SqlParameter("@DrugNum", DrugNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public DataTable Select()
        {
            try
            {
                return SqlDbHelper.RunProcedure_DataTable(ConnectionString.ConnString, "Proc_Drug_Select_All");
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public DataTable Select(string DrugTypeName, string TradeName, string DrugCode)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@DrugTypeName",DrugTypeName),
                    new SqlParameter("@DrugName",TradeName),
                    new SqlParameter("@DrugCode",DrugCode)
                };
                return SqlDbHelper.RunProcedure_DataTable(ConnectionString.ConnString, "Proc_Drug_Filter1", all);

            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public DataTable Select(string DrugType, string trade_name, string common_name, string drug_code)
        {

            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@DrugTypeName",DrugType),
                    new SqlParameter("@trade_name",trade_name),                   
                    new SqlParameter("@common_name",common_name),
                    new SqlParameter("@DrugCode",drug_code)
                };
                return SqlDbHelper.RunProcedure_DataTable(ConnectionString.ConnString, "Proc_Drug_Filter", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Insert(string DrugTypeNum, string DrugTradeName, string DrugCommonName, string UserDefinedName, string DrugCode, string Specifications, string MeasuringUnitNum, string ApprovalNum, int max, int min, float tradeprice, float retail_price)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("DrugTypeName",DrugTypeNum),
                    new SqlParameter("@TradeName",DrugTradeName),
                    new SqlParameter("@CommonName",DrugCommonName),
                    new SqlParameter("@UserDefinedName",UserDefinedName),
                    new SqlParameter("@DrugCode",DrugCode),
                    new SqlParameter("@Specifications",Specifications),
                    new SqlParameter("@MeasuringUnitName",MeasuringUnitNum),
                    new SqlParameter("@ApprovallNum",ApprovalNum),
                    new SqlParameter("@Max",max),
                    new SqlParameter("@Min",min ),
                    new SqlParameter("@Trade_Price",tradeprice),
                    new SqlParameter("@Retail_Price",retail_price),
                };
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Drug_Insert", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        #region 灼林的
        //public int Insert(int DrugTypeNum, string TradeName, string CommonName, string UserDefinedName, string DrugCode, string Specifications, int MeasuringUnitNum, string ApprovalNum,int MinQuantity,int MaxQuantity,float TradePrice,float RetailPrice)
        //{
        //    try
        //    {
        //        SqlParameter[] all = new SqlParameter[]
        //        {
        //            new SqlParameter("@DrugTypeNum",DrugTypeNum),
        //            new SqlParameter("@TradeName",TradeName),
        //            new SqlParameter("@CommonName",CommonName),
        //            new SqlParameter("@UserDefinedName",UserDefinedName),
        //            new SqlParameter("@DrugCode",DrugCode),
        //            new SqlParameter("@Specifications",Specifications),
        //            new SqlParameter("@MeasuringUnitNum",MeasuringUnitNum),
        //            new SqlParameter("@ApprovallNum",ApprovalNum),
        //            new SqlParameter("@MinQuantity",MinQuantity),
        //            new SqlParameter("@MaxQuantity",MaxQuantity),
        //            new SqlParameter("@TradePrice",TradePrice),
        //            new SqlParameter("@RetailPrice",RetailPrice)
        //        };
        //        return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Drug_Insert", all);
        //    }
        //    catch (Exception ex)
        //    {
        //        EventLog.WriteErrorLog(ex.Message);
        //        throw;
        //    }
        //}

        //public int Update(int DrugNum, int DrugTypeNum, string TradeName, string CommonName, string UserDefinedName, string DrugCode, string Specifications, int MeasuringUnitNum, string ApprovalNum, int MinQuantity, int MaxQuantity, float TradePrice, float RetailPrice)
        //{
        //    try
        //    {
        //        SqlParameter[] all = new SqlParameter[]
        //        {
        //            new SqlParameter("@DrugNum",DrugNum),
        //            new SqlParameter("@DrugTypeNum",DrugTypeNum),
        //            new SqlParameter("@TradeName",TradeName),
        //            new SqlParameter("@CommonName",CommonName),
        //            new SqlParameter("@UserDefinedName",UserDefinedName),
        //            new SqlParameter("@DrugCode",DrugCode),
        //            new SqlParameter("@Specifications",Specifications),
        //            new SqlParameter("@MeasuringUnitNum",MeasuringUnitNum),
        //            new SqlParameter("@ApprovalNum",ApprovalNum),
        //            new SqlParameter("@MinQuantity",MinQuantity),
        //            new SqlParameter("@MaxQuantity",MaxQuantity),
        //            new SqlParameter("@TradePrice",TradePrice),
        //            new SqlParameter("@RetailPrice",RetailPrice)
        //        };
        //        return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Drug_Update", all);
        //    }
        //    catch (Exception ex)
        //    {
        //        EventLog.WriteErrorLog(ex.Message);
        //        throw;
        //    }
        //}
        #endregion

        public int Update(int DrugNum, string DrugTypeName, string TradeName, string CommonName, string UserDefinedName, string DrugCode, string Specifications, string MeasuringUnitName, string ApprovalNum, int max, int min, float tradeprice, float retail_price)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@DrugNum",DrugNum),
                    new SqlParameter("@DrugTypeName",DrugTypeName),
                    new SqlParameter("@TradeName",TradeName),
                    new SqlParameter("@CommonName",CommonName),
                    new SqlParameter("@UserDefinedName",UserDefinedName),
                    new SqlParameter("@DrugCode",DrugCode),
                    new SqlParameter("@Specifications",Specifications),
                    new SqlParameter("@MeasuringUnitName",MeasuringUnitName),
                    new SqlParameter("@ApprovallNum",ApprovalNum),
                    new SqlParameter("@Max",max),
                    new SqlParameter("@Min",min ),
                    new SqlParameter("@Trade_Price",tradeprice),
                    new SqlParameter("@Retail_Price",retail_price),
                };
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Drug_Update", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Delete(int DrugNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Drug_Select", new SqlParameter[] { new SqlParameter("@DrugNum", DrugNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        #endregion
    }
}
